#lang sicp
(define x (list 'a 'b))
(define y (list 'c 'd))
(define z (append x y))

(define (append! a b)
  (if (not (pair? a))
      (error "require a pair")
      (set-cdr! (last-pair a) b))
  a)
(define (last-pair p)
  (define (iter x)
    (if (null? (cdr x)) x (last-pair (cdr x))))
  (if (not (pair? p))
      (error "require a pair -- LAST-PAIR")
      (iter p)))



(define (make-cycle x)
  (set-cdr! (last-pair x) x)
  x)
(define zz (make-cycle (list 'a 'b 'c)))


(define (reverse x)
  (define (iter x y)
    (if (null? x)
        y
        (let ((rest (cdr x)))
          (set-cdr! x y)
          (iter rest x))))
  (iter x '()))

(define v (list 'a 'b 'c'd))

(define w (reverse v))